This project is the Week 2 assignment of the data product coursera course. The project illustrates the work with Maps using leaflet, and the use of R markdown. The data set is the car crash data for the year 2017 in Allegheny County. The data set is taken from this link : https://data.wprdc.org/dataset/allegheny-county-crash-data/resource/bf8b3c7e-8d60-40df-9134-21606a451c1a.
The Interactive map illustrates : Creating a basic map. Adding multiple markers (marking the car crash location). Using customize icon to the markers. Using clusters. Using popup : The popup presents : The number of cars involves in the crash. The number of deaths (Fatalities ). The number of people who suspected as a drunk driver. The number of people who suspected in drugs use. The Map uses a control option to select and show the car accidents per specific month
knitr::opts_chunk$set(echo = TRUE)
Load libraries.
library(leaflet)
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Read 2017 Car crash data in Allegheny County. Data is loaded from : https://data.wprdc.org/datastore/dump/bf8b3c7e-8d60-40df-9134-21606a451c1a
DF<-read.csv("CarCrash2017.csv")
Subset the location of the car crash Keep the data about the month of the accident. Create a new column with the month’s names instead of a number. Since there is a large number of the crash There will be an option to show car crash per specific month For information to present in a popup : AUTOMOBILE_COUNT - the number of cars involved DRINKING_DRIVER - Alcohol involved? FATAL_COUNT - Total number of death ILLEGAL_DRUG_RELATED - Ilegal drug involvement
dfCrash<-select(DF,DEC_LAT,DEC_LONG,CRASH_MONTH,AUTOMOBILE_COUNT,DRINKING_DRIVER,FATAL_COUNT,ILLEGAL_DRUG_RELATED)
colnames(dfCrash)<-c("latitude","longitude","Month Crash","No.Cars.Involved","Drinking.Driver","Death.Count","Drug.Involved")
dfCrash<-dfCrash %>% drop_na()
dfCrash<-mutate(dfCrash,Month.Crash.Name=month.name[dfCrash$`Month Crash`])
Use a PNG file as an icon for markers on the MAP.
CarsIcon <- makeIcon("CarsIcon1.png",iconWidth = 45, iconHeight = 45)
Helper function to add markers.
####################################
# Add_Marker --- Helper function #
#Paramters :
#my_map - leaflet object - markeers will be added to this object
#lat_ln_data - Data frame with the lat and long (of the required markers
#groupname - The group name of the markers
#popup - Popup content
#IconMarker - The icon of the marker
Add_Marker <- function(my_map,lat_ln_data,groupname,content,iconMarker=CarsIcon) {
#Add markers which allow creating multiple markers, with ICON and with popup
#The cluster option is selected
addMarkers(my_map,data=lat_ln_data,clusterOptions = markerClusterOptions(),group =groupname,icon = iconMarker,popup = paste("Car involved:", content$No.Cars.Involved ,"<br>",
"Fatality:",content$Death.Count,"<br>",
"Drugs Involved :", content$Drug.Involved,"<br>",
" Alcohol involved:",content$Drinking.Driver))
#addPopups(my_map, lat_ln_data$latitude,lat_ln_data$longitude,content)
}
Create the Interactive MAP using leaflet